package cn.it58.gooditname.services

/**
 * 更新信息数据类
 */
data class UpdateInfo(
    val latestVersion: String,
    val description: String,
    val announcement: String
)

/**
 * 更新服务
 */
class UpdateService {
    private val updateUrl = "http://goodit.58it.cn/v.json"
    
    fun checkForUpdates(): UpdateInfo? {
        return try {
            val url = java.net.URL(updateUrl)
            val connection = url.openConnection()
            connection.connectTimeout = 5000
            connection.readTimeout = 5000
            
            val response = connection.getInputStream().bufferedReader().use { it.readText() }
            parseUpdateInfo(response)
        } catch (e: Exception) {
            null
        }
    }
    
    private fun parseUpdateInfo(json: String): UpdateInfo? {
        return try {
            // 简单的JSON解析
            val versionMatch = Regex(""""v"\s*:\s*"([^"]+)"""").find(json)
            val describeMatch = Regex(""""describe"\s*:\s*"([^"]+)"""").find(json)
            val gonggaoMatch = Regex(""""gonggao"\s*:\s*"([^"]+)"""").find(json)
            
            if (versionMatch != null && describeMatch != null && gonggaoMatch != null) {
                UpdateInfo(
                    latestVersion = versionMatch.groupValues[1],
                    description = describeMatch.groupValues[1],
                    announcement = gonggaoMatch.groupValues[1]
                )
            } else {
                null
            }
        } catch (e: Exception) {
            null
        }
    }
} 